package net.wanho.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.wanho.entity.Role;
import net.wanho.entity.UserRole;
import net.wanho.exception.RoleException;
import net.wanho.mapper.RoleMapper;
import net.wanho.mapper.UserInfoMapper;
import net.wanho.mapper.UserRoleMapper;
import net.wanho.service.RoleService;
import net.wanho.util.ThreadUtil;
import net.wanho.vo.PageVo;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* @author Administrator
* @description 针对表【role(角色表)】的数据库操作Service实现
* @createDate 2025-05-05 02:08:26
*/
@Service
@RequiredArgsConstructor
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService{
    private final UserRoleMapper userRoleMapper;
    private final UserInfoMapper userInfoMapper;


    @Override
    public List<Integer> getRoleIdListByUserId(Integer id) {
        return userRoleMapper.selectList(new LambdaQueryWrapper<UserRole>().eq(UserRole::getUserId, id))
                .stream().mapToInt(UserRole::getRoleId)
                .boxed().collect(Collectors.toList());
    }

    @Override
    public List<Role> getUserRoleListByUserId(Integer id) {
        List<Integer> roleIdList = getRoleIdListByUserId(id);

        return baseMapper.selectList(new LambdaQueryWrapper<Role>().in(Role::getRoleId, roleIdList));
    }

    @Override
    public PageVo<Role> rolePage(Role role, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(ObjectUtil.isNotEmpty(role.getRoleName()), Role::getRoleName, role.getRoleName())
                .like(ObjectUtil.isNotEmpty(role.getRoleCode()), Role::getRoleCode, role.getRoleCode())
                .eq(ObjectUtil.isNotEmpty(role.getRoleState()), Role::getRoleState, role.getRoleState());

        IPage<Role> page = new Page<>(pageNum, pageSize);
        List<Role> roleList = baseMapper.selectBYPage(page, queryWrapper);

        return new PageVo<>(
                page.getCurrent(),
                page.getSize(),
                page.getTotal(),
                page.getPages(),
                page.offset(),
                roleList
        );
    }

    @Override
    public void addRole(Role role) {
        if(Objects.nonNull(baseMapper.selectOne(new LambdaQueryWrapper<Role>().eq(Role::getRoleName, role.getRoleName())))) {
            throw new RoleException("角色名已存在");
        }
        baseMapper.insert(role);
    }

    @Override
    public void updateRoleById(Role role) {
        role.setUpdateBy(ThreadUtil.getUserId());
        role.setUpdateTime(LocalDateTime.now());

        updateById(role);
    }

    @Override
    public Role getRoleById(Integer id) {
        Role role = getById(id);
        role.setGetCode(userInfoMapper.selectById(role.getCreateBy()).getUserCode());
        return role;
    }

    @Override
    public List<Role> exportTable(Role role, Integer pageNum, Integer pageSize) {
        PageVo<Role> rolePageVo = rolePage(role, pageNum, pageSize);
        return rolePageVo.getResultList();
    }

}




